import javax.swing.*;
import java.awt.*;

class IncomeManagement extends JFrame {
    private JTextField incomeSourceField, incomeTimeField, incomeAmountField;
    private JButton saveButton, backButton;
    private FamilyFinanceManager mainFrame;

    public IncomeManagement(FamilyFinanceManager mainFrame) {
        this.mainFrame = mainFrame;
        setTitle("收入管理");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(300, 200);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 2));

        panel.add(new JLabel("收入来源:"));
        incomeSourceField = new JTextField();
        panel.add(incomeSourceField);

        panel.add(new JLabel("收入时间 (yyyy-MM-dd):"));
        incomeTimeField = new JTextField();
        panel.add(incomeTimeField);

        panel.add(new JLabel("收入金额:"));
        incomeAmountField = new JTextField();
        panel.add(incomeAmountField);

        saveButton = new JButton("保存");
        backButton = new JButton("返回");

        panel.add(saveButton);
        panel.add(backButton);

        add(panel, BorderLayout.CENTER);

        addListeners();
    }

    private void addListeners() {
        saveButton.addActionListener(e -> {
            //获取文本框中的内容。
            String source = incomeSourceField.getText();
            String time = incomeTimeField.getText();
            String amount = incomeAmountField.getText();
            if (!source.isEmpty() && !time.isEmpty() && !amount.isEmpty()) {
                DataManager.addIncome(source, time, amount);
                JOptionPane.showMessageDialog(this, "收入数据已保存");
                mainFrame.setVisible(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "请填写所有字段", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });

        backButton.addActionListener(e -> {
            mainFrame.setVisible(true);
            dispose();
        });
    }
}